package com.server.questionbank.snframe.util.FileUtil.operation.multipart;

import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 分片操作基类
 */
@Slf4j
public abstract class MultipartOperation {
    protected final MinioClient minioClient;
    protected final String bucket;
    protected final String object;
    protected final int chunkSize;
    protected final ExecutorService executor;
    
    protected MultipartOperation(MinioClient minioClient, String bucket, String object, int chunkSize) {
        this.minioClient = minioClient;
        this.bucket = bucket;
        this.object = object;
        this.chunkSize = chunkSize;
        this.executor = createExecutor();
    }
    
    /**
     * 执行分片操作
     */
    public abstract void execute() throws Exception;
    
    /**
     * 创建线程池
     */
    protected ExecutorService createExecutor() {
        int processors = Runtime.getRuntime().availableProcessors();
        return new ThreadPoolExecutor(
            processors,
            processors * 2,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(1000),
            new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
    
    /**
     * 关闭线程池
     */
    protected void shutdown() {
        if (executor != null && !executor.isShutdown()) {
            executor.shutdown();
        }
    }
} 